<?php

namespace App\Http\Services;
/**
 * Created by PhpStorm.
 * User: thorjiang
 * Date: 2018/5/10
 * Time: 14:40
 */

use App\Models\AppConfigModel;
use App\Models\FacilityModel;
use App\Models\FacilityTicketModel;
use App\Models\UserTicketModel;
use App\Models\TicketModel;

/**
 * Class TicketDTOBuilderService
 * @package App\Http\Services
 */
class TicketDTOBuilderService
{
    /**
     * 格式化返回值
     * @param $userId
     * @param $tickets
     * @return mixed
     */
    public static function buildTickets($userId, $tickets)
    {
        $data = [];
        $ids = array_column($tickets, 'id');
        $userTickets = UserTicketModel::where('user_id', $userId)->whereIn('ticket_id', $ids)->get()->toArray();

        if (empty($userTickets)) {
            foreach ($tickets as $ticket) {
                $data[] = static::buildTicket($userId, $ticket, 0);
            }
        } else {
            $map = arrToMap($userTickets, 'ticket_id', true);
            foreach ($tickets as $ticket) {
                $ticketId = $ticket['id'];
                $count = isset($map[$ticketId]) ? count($map[$ticketId]) : 0;
                $data[] = static::buildTicket($userId, $ticket, $count, $map);
            }
        }


        return $data;
    }

    /**
     * 格式化返回值
     * @param $userId
     * @param $tickets
     * @return mixed
     */
    public static function buildTickets2($tickets)
    {
        $data = [];
        $ids = array_column($tickets, 'id');
        $facilityTickets = FacilityTicketModel::whereIn('ticket_id', $ids)->get()->toArray();
        $facilityList = FacilityModel::all(['id', 'name'])->toArray();
        $hotList = AppConfigModel::findOneByKey(AppConfigModel::HOT_TICKET);
        $hotList = explode(',', $hotList);

        $map2 =  arrToMap($facilityList, 'id');

        if (empty($facilityTickets)) {
            $map = [];
        } else {
            $map = arrToMap($facilityTickets, 'ticket_id', true);
        }

        foreach ($tickets as &$ticket) {
            $ticketId = $ticket['id'];
            if (in_array($ticketId, $hotList)) {
                $ticket['is_hot'] = 1;
            } else {
                $ticket['is_hot'] = 0;
            }
            if (isset($map[$ticketId])) {
                $data = array_map(function ($v) use ($map2) {
                    $id = $v['facility_id'];
                    $model = $map2[$id] ?? [];

                    return ['id' => $id, 'name' => $model['name'] ?? ''];
                }, $map[$ticketId]);
                $ticket['facility'] = $data;
            } else {
                $ticket['facility'] = [];
            }
        }

        return $tickets;
    }

    /**
     * build single ticket
     * @param $userId
     * @param $ticket
     * @return mixed
     */
    public static function buildTicket($userId, $ticket, $cnt, &$map = null)
    {
        if ($ticket['type'] == TicketModel::TYPE_TIME) {
            // 显示剩余时间
            $timestamp = strtotime($ticket['valid_time']) - time();
            if ($timestamp <= 0) {
                $ticket['counter'] = 0;
            } else {
                $ticket['counter'] = $timestamp;
            }
        }
        if ($cnt < $ticket['use_limit']) {
            $ticket['is_fetch'] = 0;
        } else {
            $ticket['is_fetch'] = 1;
        }

        if (!empty($map) && isset($map[$ticket['id']])) {
            $userTickets = $map[$ticket['id']];

            $ticket['code'] = $userTickets[0]['code'];
            array_shift($userTickets);
            $map[$ticket['id']] = $userTickets;
        }

        return $ticket;
    }
}
